package org.example.cloudapply.service;

import lombok.RequiredArgsConstructor;
import org.example.cloudapply.repository.ApplicationRepository;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class CloudStatsService {
    private final ApplicationRepository applicationRepository;

    public Map<String, Integer> getOpenStackStats() {
        return convertStats(applicationRepository.getCloudStats("OpenStack"));
    }

    public Map<String, Integer> getYunhongStats() {
        return convertStats(applicationRepository.getCloudStats("云宏"));
    }

    private Map<String, Integer> convertStats(Map<String, Object> rawStats) {
        // 检查 rawStats 是否为 null 并提供默认值
        if (rawStats == null) {
            rawStats = new HashMap<>();
        }
        Map<String, Integer> result = new HashMap<>();
        result.put("totalCpu", ((Number) rawStats.getOrDefault("totalCpu", 0)).intValue());
        result.put("totalMemory", ((Number) rawStats.getOrDefault("totalMemory", 0)).intValue());
        result.put("totalGpu", ((Number) rawStats.getOrDefault("totalGpu", 0)).intValue());
        return result;
    }
}
